<?php
	if (isLoggedIn()) {
		$user = new FantasyUser($_SESSION['bl_user_id']);
	}
	else {
		$user = new FantasyUser(1);
	}	
	
	$user->getSeasonRoster();	// punimo postave
	$user->getAllStats();		// punimo statistiku
	$user->calculateTotalPoints();
	
	// podaci
	$k = $settings->currentRound;
	$allowedTransfers = false;
	
?>                

            <div class="twoCollumns">
            
            <?php
				if (!isLoggedIn()) {
					header("Location: " . $settings->base . "games");
				}
				else {
			?>
            	<div class="leftCollumn">
                	<div class="leftBox">
                    	<div class="fts_teamName">
                        	<h3><?php echo $user->imeTima; ?></h3>
                        </div>
                    	<div class="fts_description">
                        	<p>
                            	Pick your starting lineup and captain for gameweek <?= $settings->currentRound; ?>.
                            </p>
                            <p>
						    	You can make changes to your team right up until <strong>
								<?php $cR = new Round($settings->currentRound); echo $cR->getFormattedDate("M d"); ?>
								 11:00</strong>.
                            </p>

                        </div>
                        <div id="fts_rosterAndStats">
                        </div>
                    </div>
                    <h3 class="smallBoxTitle"><a>Roster</a></h3>
                    
                	<div class="leftBox">
                    	<table class="tabela" cellpadding="0" cellspacing="0">
                        <tbody>
                        
                        <?php
						$round = new Round($k);
							$round->getMecevi();
							foreach($round->mecevi as $mec) {
								if($mec->odigrana == 1) {
									$mec->domacin->getRoster();
									$mec->domacin->stats = $mec->domacin->getMatchStats($mec->id);
									
									$mec->gost->getRoster();
									$mec->gost->stats = $mec->gost->getMatchStats($mec->id);								
								}
						?>
                         	<tr>
                            	<td class="tabelaCrest"><img src="media/teams/logo/<?php echo $mec->domacin->grb; ?>" height="25" /></td>
                            	<td class="tabelaNameSchedule"><a href="team/<?php echo $mec->domacin->id; ?>/<?php echo str_replace(" ", "-", $mec->domacin->ime); ?>"><?php echo $mec->domacin->ime; ?></a></td>
                                	<?php if($mec->odigrana) {?>
                                    <td class="tabelaPointsSchedule">
                                	<a href="match/<?php echo $mec->id; ?>"><?php echo $mec->domacin->stats->pts; ?>-<?php echo $mec->gost->stats->pts; ?></a>
                                    </td>
                                    <?php } ?>
                                	<?php if(!$mec->odigrana) {?>
                                	<td class="tabelaTimeSchedule"><?php echo $round->datum; ?> u <?php echo $mec->vreme; ?></td>
                                    <?php } ?>
                            	<td class="tabelaName2Schedule"><a href="team/<?php echo $mec->gost->id; ?>/<?php echo str_replace(" ", "-", $mec->gost->ime); ?>"><?php echo $mec->gost->ime; ?></a></td>
                            	<td class="tabelaCrest"><img src="media/teams/logo/<?php echo $mec->gost->grb; ?>" height="25" /></td>
                           </tr>
                        <?php
							}
						?>
                        </tbody>
                        </table>
                    </div>
                    <h3 class="smallBoxTitle"><a>Gameweek <?php echo $k; ?></a></h3>

                </div><!-- end of left collumn -->
                
                <div class="rightCollumn">
                
					<?php include ("components/userbox.php"); ?>

                	<div class="smallBox">
                    	<table class="tabela" cellpadding="0" cellspacing="0">
                        <thead>
                            <tr>
                            	<td><?php echo $user->imeTima; ?></td>
                            	<td></td>
                            </tr>
                        </thead>
                        <tbody>
                        	<tr>
                            	<td>Manager: </td>
                                <td><?php echo $user->ime; ?></td>
                            </tr>
                        	<tr>
                            	<td>Total Points: </td>
                                <td><strong><?php echo $user->ukupnoPoena; ?></strong></td>
                            </tr>
                        </tbody>
                        </table>
                    </div>
                    <h3 class="smallBoxTitle"><a href="#">Team Info</a></h3>
                    
                	<div class="smallBox">
                    	<table class="tabela" cellpadding="0" cellspacing="0">
                        <thead>
                            <tr>
                            	<td>Kolo</td>
                            	<td>Pts</td>
                            </tr>
                        </thead>
                        <tbody>
                    <?php
						foreach($user->poeni as $kolo=>$poen) {
					?>
                        	<tr>
                            	<td class="tabelaName">Gameweek <?php echo $kolo; ?></td>
                            	<td class="tabelaPoints"><?php echo $poen; ?></td>
                            </tr>
                    <?php
						}
					?>
                        </tbody>
                        </table>
                    </div>
                    <h3 class="smallBoxTitle"><a href="#">Points History</a></h3>
                    
                	<div class="smallBox">
                    	<div class="transfersTop">
						<?php if ($allowedTransfers) { ?>
                        	<a rel="pg">PG</a>
                        	<a rel="sg">SG</a>
                        	<a rel="sf">SF</a>
                        	<a rel="pf">PF</a>
                        	<a rel="c">C</a>
                        <?php } else { ?>
                            Transfers are now closed. Transfers for Gameweek 3 will be available soon.
                        <?php } ?>
                        </div>
                        
                        <div id="transfersPlayers">
                        </div>
                    </div>
                    <h3 class="smallBoxTitle"><a href="#">Transfers</a></h3>
                    
                    <script type="text/javascript">
					
						$(document).ready(
							function() {
								$("div.transfersTop a").click(
									function() {
										$("div.transfersTop a").removeClass("activeA");
										$(this).addClass("activeA");
										$.get("components/playersByPosition.php", { position: $(this).attr("rel") } ).done(function(data) {
											$("#transfersPlayers").html(data);
										});;
									}
								);
								
								$.ajax({
									type: "GET",
									url: "<?= $settings->base; ?>components/fantasyRoster.php",
									success: function(data) { $("div#fts_rosterAndStats").html(data); }
								});
							}
						);
					
					</script>
                    
                    <script type="text/javascript">
					
						$("a.transferPlayerA").live("click", function() { 
						var podaci = $(this).attr("rel").split('-');
							$.ajax({
								type: "GET",
								url: "<?= $settings->base; ?>components/fantasyRoster.php",
								data: "plr=" + podaci[0] + "&pos=" + podaci[1],
								success: function(data) { $("div#fts_rosterAndStats").html(data); }
							});
						});
					
						$("select#captain").live("change", function() { 
						var igrac = $(this).attr("value");
							$.ajax({
								type: "GET",
								url: "<?= $settings->base; ?>components/fantasyRoster.php",
								data: "plr=" + igrac + "&pos=capt",
								success: function(data) { $("div#fts_rosterAndStats").html(data); }
							});
						});
					
					</script>
                    

                    
                </div>
            </div><!-- end of middle_content -->
            
            <?php } ?>